package com.wty.mianshi.circulation;


import com.wty.mianshi.common.Node;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * 从一堆链表中，删除数据为x的节点
 */
public class LinkedDelete {


    public  <T> Node<T> delete(Node<T> head,T value){
        //特殊情况1
        if(head==null){
            return null;
        }

        //特殊情况2，head的value就是要删除的value  还有 2，2，2，3，4中删除2的这种情况，要循环判断头部节点
        while(head.getData()==value){
            head=head.getNext();
        }




        Node<T> prev=head;

        while (prev.getNext()!=null){
            //相等
            if(prev.getNext().getData()==value){
                prev.setNext(prev.getNext().getNext());
            }else {
                //不相等
                prev=prev.getNext();
            }
        }
        return head;

    }





    public static void main(String args[]){

        LinkedCreator creator=new LinkedCreator();
        LinkedDelete delete=new LinkedDelete();


        Node node=delete.delete(creator.createLinkedList(Arrays.asList(1,2,3,2,4,2,5)),2);
        System.out.println("正常的测试");
        creator.printLinkedList(node);

        Node node2=delete.delete(creator.createLinkedList(Arrays.asList(2,2,3,2,4,2,5)),2);
        System.out.println("头部重复测试");
        creator.printLinkedList(node2);

        Node node3=delete.delete(creator.createLinkedList(Arrays.asList(1,2,3,2,4,2,5,2,2,2)),2);
        System.out.println("尾部重复测试");
        creator.printLinkedList(node3);

        Node node4=delete.delete(creator.createLinkedList(new ArrayList()),2);
        System.out.println("链表为空测试");
        creator.printLinkedList(node4);
    }
}
